<template>
  <!--插片记录-->
  <div class="app-container">
    <el-form class="jc_search_wrap" v-model="searchList">
      <label>单晶编号</label><el-input v-model="searchList.crystalNo"></el-input>
      <label>机台号</label><el-input v-model="searchList.capianNo"></el-input>
      <label>操作人</label><el-input v-model="searchList.capianName"></el-input>
      <label>备注</label><el-input v-model="searchList.description"></el-input>
      <br />
      <!-- <label>插片时间</label>
      <el-input type="date" v-model="searchList.date1"></el-input>至<el-input type="date" v-model="searchList.date2"></el-input> -->
              <label style="width:100px">插片开始时间:</label>
              <el-date-picker
                v-model="startDate"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
              <label style="width:100px">插片结束时间:</label>
              <el-date-picker
                v-model="endDate"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
      <el-button @click="searchRows"><i class="el-icon-search"></i>查询</el-button>
    </el-form>
    <el-button-group class="jc_btn_wrap jc_btn_left">
      <el-button @click="editRow"><i class="jc_icon"></i>编辑</el-button>
      <el-button @click="deleteRows"><i class="jc_icon"></i>删除</el-button>
      <el-button @click="exportRows"><i class="jc_icon"></i>导出</el-button>
      <el-button @click=""><i class=""></i>打印</el-button>
    </el-button-group>
    <el-table :data="list"  :height="tableHeight"  border fit highlight-current-row class="jc_table_wrap"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="80px">
      </el-table-column>
      <el-table-column label='单晶编号' fixed width="230">
        <template scope="scope">
          {{scope.row.crystalNo}}
        </template>
      </el-table-column>
      <el-table-column label='插片机号'>
        <template scope="scope">
          {{scope.row.capianNo }}
        </template>
      </el-table-column>
      <el-table-column label='插片时间'>
        <template scope="scope">
          {{scope.row.capianDate }}
        </template>
      </el-table-column>
      <el-table-column label='插片裂片数'>
        <template scope="scope">
          {{scope.row.capianLiepian }}
        </template>
      </el-table-column>
      <el-table-column label='插片员'>
        <template scope="scope">
          {{scope.row.capianName }}
        </template>
      </el-table-column>
      <el-table-column label='插片数'>
        <template scope="scope">
          {{scope.row.capianCount }}
        </template>
      </el-table-column>
      <el-table-column label='插片碎片数'>
        <template scope="scope">
          {{scope.row.capianSuipian }}
        </template>
      </el-table-column>
      <el-table-column label='备注'>
        <template scope="scope">
          {{scope.row.description }}
        </template>
      </el-table-column>
    </el-table>
   <!-- <el-pagination layout="prev, pager, next" :total="total" @current-change="changePage" :page-size="10"/>-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="changePage"
      :current-page="currentPage"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
     <el-dialog title="编辑" :visible.sync="edit_visible" class="jc_dialog_wrap">
      <el-form :model="form_edit" label-width="120px">
        <el-form-item label="单晶编号" >
          <el-input v-model="form_edit.crystalNo"></el-input>
        </el-form-item>
        <el-form-item label="插片机号" >
          <el-input v-model="form_edit.capianNo"></el-input>
        </el-form-item>
        <el-form-item label="插片时间" >
          <el-input v-model="form_edit.capianDate"></el-input>
        </el-form-item>
        <el-form-item label="插片裂片数" >
          <el-input v-model="form_edit.capianLiepian"></el-input>
        </el-form-item>
        <el-form-item label="插片员" >
          <el-input v-model="form_edit.capianName"></el-input>
        </el-form-item>
        <el-form-item label="插片机号" >
          <el-input v-model="form_edit.capianCount"></el-input>
        </el-form-item>
        <el-form-item label="插片碎片数" >
          <el-input v-model="form_edit.capianSuipian"></el-input>
        </el-form-item>
        <el-form-item label="备注" >
          <el-input v-model="form_edit.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEditForm">确定</el-button>
        <el-button @click="edit_visible=false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

  import * as Illustration from "../../api/report/illustration";
  var moment = require('moment');
  export default {
    components: {

    },
    data() {
      return {
        searchList: {pageSize: '',crystalNo: '',date1: '',date2: '',capianNo: '',capianName: '',description: ''},
        list: [],
        total: 0,
        create_visible: false,
        detail_visible: false,
        edit_visible: false,
        form_create: {},
        form_detail: {},
        form_edit: {},
        multipleSelection: [],
        crystalNo: '',
        // date1: '',
        // date2: '',
        startDate:'',
        endDate:'',
        pageSize: 10,
        currentPage: 1,
        value1: '08:30',
      }

    },
    created() {
      // this.fetchData();
    },
    methods: {
      fetchData(p=1) {
        this.listLoading = true;
        this.searchList.pageNum = this.currentPage = p;
        this.searchList.pageSize = this.pageSize;
        Illustration.getList(this.searchList).then(data => {
          this.list = data.rows;
          this.total = parseInt(data.total);
          this.listLoading = false;
        })

      },
      searchRows() {
        // this.searchList.date1 = moment(this.startDate).format('YYYY-MM-DD HH:mm:ss');
        // this.searchList.date2 = moment(this.endDate).format('YYYY-MM-DD HH:mm:ss');
        if( !this.startDate == ''){
          this.searchList.date1 = moment(this.startDate).format('YYYY-MM-DD HH:mm:ss');
        }else{
          this.searchList.date1 = ''
        }
        if(!this.endDate == ''){
            this.searchList.date2 = moment(this.endDate).format('YYYY-MM-DD HH:mm:ss');
        }else{
          this.searchList.date2 = ''
        }
        this.fetchData()
      },
      deleteRows() {
        if(this.multipleSelection.length===0) {
          alert('请选择行');
        }else {
          var ids = [];
          for(var i in this.multipleSelection) {
            ids.push(this.multipleSelection[i].id)
          }
          this.$confirm('此操作将永久删除该行数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            Illustration.delRows({ids: ids.join(',')}).then(data => {
              this.fetchData();
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
            })

          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });
        }
      },
      editRow() {
        if(this.multipleSelection.length === 0) {
          alert('请先勾选行');
        }else if(this.multipleSelection.length >= 2) {
          alert('每次只能查看一行数据，请只勾选一行');
        }else if(this.multipleSelection.length === 1){
          this.edit_visible = true;
          Illustration.detailRow(this.multipleSelection[0].id).then(data => {
            this.form_edit = data.msg;
          });
        }
      },
      submitEditForm() {
        Illustration.updateRow(this.form_edit).then(data => {
          this.edit_visible = false;
          this.fetchData();
        })
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      changePage(p) {
        this.fetchData(p)
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.currentPage = 1;
        this.fetchData(1);

      },
      exportRows() {
        window.location.href=process.env.BASE_API+'/illustration/export?crystalNo='+this.searchList.crystalNo+'&date1='+this.searchList.date1+'&date2='+this.searchList.date2;
      },
    },
    computed: {
      tableHeight() {
        const filterHeight = 28
        var tableHeight = this.getTableHeight(filterHeight)
        return tableHeight-30;
      }
    },
    mounted () {
      this.startDate = moment.utc(moment().subtract(1, 'days').format('YYYY-MM-DD 00:30:00')).format();
      this.endDate = moment.utc(moment().format('YYYY-MM-DD 00:30:00')).format();
      this.searchRows();
    }
  }
</script>
